package cn.medemede.dao;

import cn.medemede.dbc.DatabaseConnection;
import cn.medemede.model.UsersBean;

import java.sql.*;
import java.util.Calendar;

public class UsersDao{
    public static boolean creat(UsersBean usersBean) throws Exception {
        Connection connection= DatabaseConnection.getConnection();
        String InsertSQL = "INSERT INTO users VALUES (?,?,?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement=connection.prepareStatement(InsertSQL);
        preparedStatement.setString(1,usersBean.getUsername());
        preparedStatement.setString(2,usersBean.getZname());
        preparedStatement.setString(3,usersBean.getSex());
        preparedStatement.setString(4,usersBean.getBorth());
        preparedStatement.setString(5,usersBean.getPhone());
        preparedStatement.setString(6,usersBean.getZposition());
        preparedStatement.setString(7,usersBean.getEmail());
        preparedStatement.setString(8,usersBean.getFavorite());
        preparedStatement.setString(9,usersBean.getCharacter());
        int flag=preparedStatement.executeUpdate();
        connection.close();
        preparedStatement.close();
        return flag>0;
    }


    public static UsersBean find(String username) throws Exception {
        Connection connection=DatabaseConnection.getConnection();
        String FindSQL = "select * from users where username=?";
        PreparedStatement preparedStatement=connection.prepareStatement(FindSQL);
        preparedStatement.setString(1,username);
        ResultSet rs=preparedStatement.executeQuery();
        UsersBean usersBean=new UsersBean();
        usersBean.setUsername(username);
        while (rs.next()){
            usersBean.setZname(rs.getString("zname"));
            usersBean.setSex(rs.getString("sex"));
            usersBean.setBorth(rs.getString("borth"));
            usersBean.setPhone(rs.getString("phone"));
            usersBean.setZposition(rs.getString("zposition"));
            usersBean.setEmail(rs.getString("email"));
            usersBean.setFavorite(rs.getString("favorite"));
        }

        //计算年龄
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DATE);
        String borth=usersBean.getBorth();
        int age;
        int u_y = Integer.parseInt(borth.substring(0, 4));
        int u_m;
        if ('0' == borth.charAt(5)) {
            u_m = (int) borth.charAt(6);
        } else {
            u_m = Integer.parseInt(borth.substring(5, 7));
        }
        int u_d;
        if ('0' == borth.charAt(8)) {
            u_d = (int) borth.charAt(9);
        } else {
            u_d = Integer.parseInt(borth.substring(8, 10));
        }

        if (month > u_m) {
            age = year - Integer.parseInt(borth.substring(0, 4));
        } else if (day >= u_d) {
            age = year - Integer.parseInt(borth.substring(0, 4));
        } else {
            age = year - Integer.parseInt(borth.substring(0, 4)) - 1;
        }

        usersBean.setAge(age);
        rs.close();
        connection.close();
        preparedStatement.close();
        return usersBean;
    }

    public static boolean update(UsersBean usersBean) throws Exception {
        Connection connection=DatabaseConnection.getConnection();
        String UpdateSQL = "UPDATE users SET zname=?,sex=?,borth=?,phone=?,zposition=?,email=?,favorite=?,character=? WHERE username=?";
        PreparedStatement preparedStatement=connection.prepareStatement(UpdateSQL);
        preparedStatement.setString(1,usersBean.getZname());
        preparedStatement.setString(2,usersBean.getSex());
        preparedStatement.setString(3,usersBean.getBorth());
        preparedStatement.setString(4,usersBean.getPhone());
        preparedStatement.setString(5,usersBean.getZposition());
        preparedStatement.setString(6,usersBean.getEmail());
        preparedStatement.setString(7,usersBean.getFavorite());
        preparedStatement.setString(8,usersBean.getCharacter());
        preparedStatement.setString(9,usersBean.getUsername());
        System.out.println(usersBean.getUsername());
        int flag=preparedStatement.executeUpdate();
        connection.close();
        preparedStatement.close();
        return flag>0;
    }
}
